package com.linkage.educloud.ah.view.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.mvp.contract.mine.IChangePwdContract;
import com.linkage.educloud.ah.mvp.presenter.mine.ChangePwdPresenter;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.zzhou.common.library.util.ToastShow;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/11.
 */

public class ChangePwdActivity extends BasicTitleActivity implements IChangePwdContract.IView {

    @BindView(R.id.ac_changepwd_old_pwd)
    EditText ac_changepwd_old_pwd;
    @BindView(R.id.ac_changepwd_new_pwd)
    EditText ac_changepwd_new_pwd;
    @BindView(R.id.ac_changepwd_renew_pwd)
    EditText ac_changepwd_renew_pwd;
    @BindView(R.id.ac_changepwd_submit)
    Button ac_changepwd_submit;

    IChangePwdContract.IPresenter iPresenter;

    public static void launch(Context context) {
        Intent intent = new Intent(context, ChangePwdActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_changepwd;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iPresenter = new ChangePwdPresenter(this, this);
        submit();
    }

    private void submit() {
        ac_changepwd_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ac_changepwd_old_pwd.getText().toString().trim().isEmpty()) {
                    ToastShow.show(ChangePwdActivity.this, "请输入原密码");
                    return;
                }
                if (ac_changepwd_new_pwd.getText().toString().trim().isEmpty()) {
                    ToastShow.show(ChangePwdActivity.this, "请输入新密码");
                    return;
                }
                if (ac_changepwd_renew_pwd.getText().toString().trim().isEmpty()) {
                    ToastShow.show(ChangePwdActivity.this, "请重复新密码");
                    return;
                }
                if (!ac_changepwd_new_pwd.getText().toString().trim().equals(ac_changepwd_renew_pwd.getText().toString().trim().isEmpty())) {
                    ToastShow.show(ChangePwdActivity.this, "两次密码不一致");
                    return;
                }
                if (!ac_changepwd_new_pwd.getText().toString().trim().equals("^[0-9A-Za-z]{6,16}$")) {
                    ToastShow.show(ChangePwdActivity.this, "密码6-16位，且只支持数字，大小写字母");
                    return;
                }
                showZloading();
                iPresenter.updatePassword(ac_changepwd_old_pwd.getText().toString().trim(), ac_changepwd_renew_pwd.getText().toString().trim());
            }
        });
    }

    public void hidezloading() {
        hideZloading();
    }
}
